package com.vcaresolution.logistic.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;

import com.vcaresolution.logistic.form.UserForm;
import com.vcaresolution.logistic.to.CompanyTO;
import com.vcaresolution.logistic.to.DataHolderTO;
import com.vcaresolution.logistic.to.UserProfileTO;
import com.vcaresolution.logistic.util.ApplicationConstants;

/**
 * <code>UserController</code> is used to track all user related actions
 *
 * @author Snehal Shah
 * @version 1.0
 * @since ShipSoft 1.0
 */
@Controller
public class UserController extends BaseController {

	@ModelAttribute(value=ApplicationConstants.USERFORM_MODELATTRIBUTE)
	public UserForm createForm(){
		return new UserForm();
	}
	
	/**
	 * This method is called when the GET request has been made to the ADDCompany Request
	 */
	@RequestMapping(value=ApplicationConstants.ADD_USER_URL,method = RequestMethod.GET)
	public ModelAndView loadAddUser() {
		/**#editedByVishal*/
		
		List<CompanyTO> companyList = getServiceRegistry().getCompanyService().findByAll();
		/*List<DataHolderTO> branchList = null;
		if(companyList!=null && !companyList.isEmpty())
		{
			branchList = getServiceRegistry().getCompanyService().getAssociatedBranchList(companyList.get(0).getCompanyid(), DataHolderTO.class);	
		}
		else
		{
			branchList = new ArrayList<DataHolderTO>();
		}*/
		
		/*List<DataHolderTO> departmentList = null;
		if(!branchList.isEmpty())
		{
			departmentList = getServiceRegistry().getBranchService().getAssociatedDepartmentList(branchList.get(0).getFieldId(),DataHolderTO.class);
		}
		else
		{
			departmentList = new ArrayList<DataHolderTO>();
		}*/
		
		ModelAndView modelAndView = new ModelAndView();
		
		modelAndView.setViewName(ApplicationConstants.ADD_USER_VIEW);
		modelAndView.addObject(ApplicationConstants.USERFORM_MODELATTRIBUTE, new UserForm());
		modelAndView.addObject(ApplicationConstants.USERGROUPLIST_MODELATTRIBUTE, getServiceRegistry().getUserGroupService().findByAll());
		modelAndView.addObject(ApplicationConstants.COMPANYLIST_MODELATTRIBUTE,companyList);
//		modelAndView.addObject(ApplicationConstants.BRANCHLIST_MODELATTRIBUTE,branchList);
//		modelAndView.addObject(ApplicationConstants.DEPARTMENTLIST_MODELATTRIBUTE,departmentList);
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the POST request has been made to the ADDCompany Request
	 */
	@RequestMapping(value=ApplicationConstants.ADD_USER_URL,method = RequestMethod.POST)
	public ModelAndView processAddUser(@Valid UserForm userForm, final BindingResult result,final HttpServletRequest request,final RedirectAttributes redirectAttrs) {
		ModelAndView modelAndView = new ModelAndView();
		
		System.out.println("Inside Controller :: "+result.hasErrors());
		
		if( !result.hasErrors() ){
			getServiceRegistry().getUserService().add(userForm);
			
			modelAndView.setView(new RedirectView(ApplicationConstants.LIST_USER_URL, true, true, false));
		}
		else{
			modelAndView.addObject(ApplicationConstants.COMPANYLIST_MODELATTRIBUTE, getServiceRegistry().getCompanyService().findByAll());
			modelAndView.addObject(ApplicationConstants.USERGROUPLIST_MODELATTRIBUTE, getServiceRegistry().getUserGroupService().findByAll());
		}
		
		return modelAndView;
	}
	/**
	 * This method is called when the GET request has been made to the ListUser Request
	 */
	@RequestMapping(value=ApplicationConstants.LIST_USER_URL,method = RequestMethod.GET)
	public ModelAndView list() {
		final ModelAndView modelAndView = new ModelAndView();
		
		List<UserProfileTO> userList = getServiceRegistry().getUserService().findByAll();
		
		modelAndView.addObject(ApplicationConstants.USERLIST_MODELATTRIBUTE, userList);
		modelAndView.setViewName(ApplicationConstants.LIST_USER_VIEW);
		return modelAndView;
	}
	
	/**
	 * This method is called when the GET request has been made to the EditUser Request
	 */
	@RequestMapping(value=ApplicationConstants.EDIT_USER_URL,method = RequestMethod.GET)
	public  ModelAndView loadEditUser(@RequestParam(value="userId") String userID) {
		ModelAndView modelAndView = new ModelAndView();
		
		Object userObject = getServiceRegistry().getUserService().findById(Integer.parseInt(userID));
		
		
		modelAndView.addObject(ApplicationConstants.USERFORM_MODELATTRIBUTE, userObject);
		modelAndView.addObject(ApplicationConstants.COMPANYLIST_MODELATTRIBUTE, getServiceRegistry().getCompanyService().findByAll());
		modelAndView.addObject(ApplicationConstants.USERGROUPLIST_MODELATTRIBUTE, getServiceRegistry().getUserGroupService().findByAll());
		modelAndView.addObject(ApplicationConstants.BRANCHLIST_MODELATTRIBUTE, getServiceRegistry().getBranchService().findByAll());
		modelAndView.addObject(ApplicationConstants.DEPARTMENTLIST_MODELATTRIBUTE,getServiceRegistry().getDepartmentService().findByAll());
		
		modelAndView.setViewName(ApplicationConstants.EDIT_USER_VIEW);
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the POST request has been made to the EditUser Request
	 */
	@RequestMapping(value=ApplicationConstants.EDIT_USER_URL,method = RequestMethod.POST)
	public ModelAndView processEditUser(@Valid UserForm userForm, final BindingResult result,final HttpServletRequest request,final RedirectAttributes redirectAttrs) {
		ModelAndView modelAndView = new ModelAndView();
		
		System.out.println("Inside Controller :: "+result.hasErrors());
		
		if( !result.hasErrors() ){
			getServiceRegistry().getUserService().update(userForm);
			
			modelAndView.setView(new RedirectView(ApplicationConstants.LIST_USER_URL, true, true, false));
		}
		else{
			modelAndView.addObject(ApplicationConstants.COMPANYLIST_MODELATTRIBUTE, getServiceRegistry().getCompanyService().findByAll());
			modelAndView.addObject(ApplicationConstants.USERGROUPLIST_MODELATTRIBUTE, getServiceRegistry().getUserGroupService().findByAll());
		}
		
		return modelAndView;
	}
	
}
